package com.itxiuyixiu.homework.config;

import com.itxiuyixiu.homework.admin.controller.AdminMessageController;
import com.itxiuyixiu.homework.entity.dto.AcademyDto;
import com.itxiuyixiu.homework.entity.dto.MajorDto;
import com.itxiuyixiu.homework.entity.dto.StudentDto;
import com.itxiuyixiu.homework.entity.dto.TeacherDto;
import com.itxiuyixiu.homework.entity.enums.Sex;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.util.concurrent.*;

/**
 * @author 黄磊
 * @date 2020/3/12
 */
@Component
public class TestData implements ApplicationRunner {

    @Value("${spring.profiles.active}")
    private String profile;

    private final AdminMessageController adminMessageController;
    private final ExecutorService threadPool = Executors.newFixedThreadPool(5);

    private int academyNumber = 5;
    private int majorNumber = 5;
    private int studentNumber = 5;
    private int teacherNumber = 5;

    public TestData(AdminMessageController adminMessageController) {
        this.adminMessageController = adminMessageController;
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        if ("pro".equals(profile)) {
            return;
        }
        addAcademy();
        addMajor();
        addStudent();
        addTeacher();
        threadPool.awaitTermination(3, TimeUnit.MINUTES);
        threadPool.shutdown();
    }

    void addAcademy() {
        for (int i = 1; i <= academyNumber; i++) {
            AcademyDto dto = new AcademyDto();
            dto.setAcademyName("学院" + i);
            threadPool.submit(() -> {
                adminMessageController.addAcademyMessage(dto);
            });
        }
    }

    void addMajor() throws CloneNotSupportedException {
        MajorDto dto = new MajorDto();
        for (int i = 1; i <= academyNumber; i++) {
            dto.setAcademyId(i);
            for (int j = 1; j <= majorNumber; j++) {
                dto.setMajorName("学院" + i + "—专业" + j);
                MajorDto majorDto = (MajorDto) dto.clone();
                threadPool.submit(() -> {
                    adminMessageController.addMajorMessage(majorDto);
                });
            }
        }
    }

    void addStudent() throws CloneNotSupportedException {
        StudentDto dto = new StudentDto();
        for (int i = 1; i <= academyNumber; i++) {
            dto.setAcademyId(i);
            for (int j = 1; j <= majorNumber; j++) {
                dto.setMajorId(j);
                for (int k = 1; k <= studentNumber; k++) {
                    dto.setStudentName("学院" + i + "—专业" + j + "—学生" + k);
                    dto.setSex(Sex.values()[k % 2]);
                    dto.setIdentityCard(String.format("%02d", i) + String.format("%02d", j) + String.format("%02d", k) + "199803090000");
                    dto.setStudentContact("学生联系方式");
                    dto.setStudentClass("学生班级");
                    StudentDto studentDto = (StudentDto) dto.clone();
                    threadPool.submit(() -> {
                        adminMessageController.addStudentMessage(studentDto);
                    });
                }
            }
        }
    }

    void addTeacher() throws CloneNotSupportedException {
        TeacherDto dto = new TeacherDto();
        for (int i = 1; i <= academyNumber; i++) {
            dto.setAcademyId(i);
            for (int j = 1; j <= teacherNumber; j++) {
                dto.setTeacherName("学院" + i + "—老师" + j);
                TeacherDto teacherDto = (TeacherDto) dto.clone();
                threadPool.submit(() -> {
                    adminMessageController.addTeacherMessage(teacherDto);
                });
            }
        }
    }
}
